package org.openintents.droidcon.cloud.model;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.openintents.droidcon.cloud.PMF;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable
public class Slot {
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key id;
	
	@Persistent
	private String roomAlias;
	@Persistent
	private String title;
	@Persistent
	private String description;
	@Persistent
	private String speaker;
	
	@NotPersistent
	private static PersistenceManager pm;
	public Slot(String title, String description, String speaker,
			String alias) {
		this.title = title;
		this.description = description;
		this.speaker = speaker;
		this.roomAlias = alias;
	}
	public String getRoomAlias() {
		return roomAlias;
	}
	public void setRoomAlias(String roomAlias) {
		this.roomAlias = roomAlias;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getSpeaker() {
		return speaker;
	}
	public void setSpeaker(String speaker) {
		this.speaker = speaker;
	}
	public Key getId() {
		return id;
	}
	
	/**
	 * Returns the PersistenceManager for the Schedule Class, creates one if not
	 * existent or closed
	 * 
	 * @return PersistenceManager
	 */
	private static PersistenceManager getPersistenceManager() {
		if (pm == null) {
			pm = PMF.get().getPersistenceManager();
		} else if (pm.isClosed()) {
			pm = PMF.get().getPersistenceManager();
		}
		return pm;
	}
	/**
	 * Deletes the Schedule object from the persistent storage
	 * 
	 * @return
	 */
	public boolean delete() {
		boolean wasSuccessful;
		try {
			getPersistenceManager().deletePersistent(this);
			wasSuccessful = true;
		} catch (Exception e) {
			wasSuccessful = false;
		} finally {
			getPersistenceManager().close();
		}
		return wasSuccessful;
	}

	
}
